<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
$intBT = (int)$_data["Bet_Type"];
$intGID = (int)$_data["GambleID"];
$strNumber = trim($_data["strNumber"]);

$flOdds = (float)$_data["Odds"];
$flGapOdds = (float)$_data["GapOdds"];
$isGapUpdate = (int)$_data["isGapUpdate"];


if($intBT < 1 || $intGID < 1 || $intLID < 1){
	$GLOBALS["errormessage"] = "Data Error!!";
	$GLOBALS["errorhidden"] = "\nBetType:".$intBT."\nGambleID:".$intGID."\nLottoID:".$intLID."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

$arrNum = explode(",", $strNumber);
//若數字是空陣列，就不處理
if(sizeof($arrNum) == 0){
	$GLOBALS["errormessage"] = "No Number Data!!";
	$GLOBALS["errorhidden"] = "\nNumber:".$strNumber."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}
foreach($arrNum as $k => $v){
	$arrNum[$k] = (int)$v;
}

$arrBT = array();
if($intBT == 1 || $intBT == 28){
	$arrBT = array(1, 28);
}elseif($intBT == 4 || ($intBT > 20 && $intBT < 27) || $intBT == 31){
	$arrBT = array($intBT);
}
$arrOID = array();
PMA_mysql_query("BEGIN", $_ConnMaster);

if($isGapUpdate == "1"){
	//以特碼 A 改盤
	if($intBT == 28)$intBT = 1;

	$strSQL = "select Number, Odds from `bet_TodayGame".$intLID."` where Bet_Type = ".$intBT." and GambleID = ".$intGID." and Number in (".implode(",", $arrNum).")";
	if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
		@PMA_mysql_query("ROLLBACK", $_ConnMaster);
		ErrMsgOutputHTML("Error", "", 1);
	}

	while($Rows = PMA_mysql_fetch_object($RS)){
		$flOdds = $Rows -> Odds + $flGapOdds;
		if($flOdds < 1)$flOdds = 1;

		foreach($arrBT as $intBT2){
			$flOddsTmp = $flOdds;
			if($intBT2 == 28){
				$flABGapOdds = $_flABGapOdds;
				if($flOddsTmp <= $_flAOdds2)$flABGapOdds = $_flABGapOdds2;
				if($flOddsTmp <= $_flAOdds3)$flABGapOdds = $_flABGapOdds3;				
				$flOddsTmp += $flABGapOdds;
			}
			$flOddsTmp = ConvertOdds($intLType, $intBT2, $flOddsTmp);

			//找出 Apoint
			$intAN = (int)$_arrGLineNumToApointNum[$intLID][$intBT2][$Rows -> Number];

			if($_arrBetParam[$intBT2][$intAN]["maxOdds"] < $flOddsTmp)$flOddsTmp = $_arrBetParam[$intBT2][$intAN]["maxOdds"];

			if(!$intOID = InsertBetTypeOdds($intGID, $intLID, $intBT2, $Rows -> Number, $flOddsTmp, 0, $_SESSION["Admin_Info"] -> Account)){
				@PMA_mysql_query("ROLLBACK", $_ConnMaster);
				ErrMsgOutputHTML("Error", "", 1);
			}
			if(!UpdateTodayGame($intGID, $intLID, $intBT2, $Rows -> Number, $intOID, $flOddsTmp, ", tolJumpStake = 0")){
				@PMA_mysql_query("ROLLBACK", $_ConnMaster);
				ErrMsgOutputHTML("Error", "", 1);
			}
			$arrOID[] = $intOID;
		} //foreach($arrBT as $intBT){
	} //while($Rows = PMA_mysql_fetch_object($RS)){

}else{ //if($isGapUpdate == "1"){

	if($intBT == 1 || $intBT == 28){	//特碼 A B 轉換成特碼A改盤
		if($intBT == 28){
			$flABGapOdds = $_flABGapOdds;
			if($flOdds <= $_flBOdds2)$flABGapOdds = $_flABGapOdds2;
			if($flOdds <= $_flBOdds3)$flABGapOdds = $_flABGapOdds3;
			$flOdds -= $flABGapOdds;
		}
		$flABGapOdds = $_flABGapOdds;
		if($flOdds <= $_flAOdds2)$flABGapOdds = $_flABGapOdds2;
		if($flOdds <= $_flAOdds3)$flABGapOdds = $_flABGapOdds3;
		$intBT = 1;
	}

	if($flOdds < 1)$flOdds = 1;
	include_once(__Common_Path."/gambletype/GambleType1_".$intLID.".inc.php");
	$arrGLineAllOdds1 = $_arrGambleTypeAllOdds[$intBT];

	include_once(__Common_Path."/gambletype/GambleType".$_data["GameType"]."_".$intLID.".inc.php");
	$arrGLineAllOdds2 = $_arrGambleTypeAllOdds[$intBT];

	foreach($arrBT as $intSubBT){
		$intAN = $_arrGLineNumToApointNum[$intLID][$intBT][$intNum];
		if($intAN == null)$intAN = 0;
		$flOddsTmp = $flOdds;
		if($_arrBetParam[$intBT][$intAN]["maxOdds"] < $flOddsTmp)$flOddsTmp = $_arrBetParam[$intBT][$intAN]["maxOdds"];
		$flOddsTmp = ConvertOdds($intLType, $intBT, $flOddsTmp);

		$A_AllOdds = $arrGLineAllOdds1[$intAN];
		$preAllOdds = $arrGLineAllOdds2[$intAN];
		
		if($intSubBT == 28)$flOddsTmp += $flABGapOdds;
		//print $intSubBT."=".$flOddsTmp."\n";

		$flOddsTmp -= ($preAllOdds - $A_AllOdds)/2;
		
		foreach($arrNum as $intNum){
			if(!$intOID = InsertBetTypeOdds($intGID, $intLID, $intSubBT, $intNum, $flOddsTmp, 0, $_SESSION["Admin_Info"] -> Account)){
				@PMA_mysql_query("ROLLBACK", $_ConnMaster);
				ErrMsgOutputHTML("Error", "", 1);
			}
			if(!UpdateTodayGame($intGID, $intLID, $intSubBT, $intNum, $intOID, $flOddsTmp, ", tolJumpStake = 0")){
				@PMA_mysql_query("ROLLBACK", $_ConnMaster);
				ErrMsgOutputHTML("Error", "", 1);
			}
			$arrOID[] = $intOID;
		} //for($intNum = 0; $intNum < sizeof($arrNum); $intNum++){
	}

} // //if($isGapUpdate == "1"){
@PMA_mysql_query("COMMIT", $_ConnMaster);

//產生 TodayGame File
foreach($arrBT as $intSubBT){
	if(!MakeTodayGameFile($intLID, $intSubBT))ErrMsgOutputHTML("Error", "", 1);
}
$strLog = "樂透種類：".$intLID."\n";
$strLog .= "操盤序號：".$intGID."\n";
$strLog .= "下注種類：".$intBT."\n";
$strLog .= "賠率序號：".join(",", $arrOID)."\n";
$strLog .= "賠率：".$flOdds."\n";
$strLog .= "調整賠率差：".$flGapOdds."\n";
$strLog .= "調整方式：".$isGapUpdate."\n";
$strLog .= "號碼：".$strNumber."\n";
AddData_Log($_SESSION["Admin_Info"] -> ID, "批次更改賠率", $strLog);
/*=================================================================*/
header("Cache-Control: no-store, no-cache, must-revalidate");	  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");															  // HTTP/1.0
?><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript">
<!--
parent.RefreshType();
//-->
</script>